
package com.feib.soeasy.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.PaymentCanceledReasonDao;
import com.feib.soeasy.dao.RealAccountDao;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.PaymentCanceled;
import com.feib.soeasy.model.PaymentCanceledReason;
import com.feib.soeasy.model.RealAccount;
import com.feib.soeasy.pagination.PaginatedList;


/**
 * @title (#)RealAccountHibernateDao.java<br>
 * @description <br>
 * @author Karl Chen<br>
 * @version 1.0.0 2012/07/21
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2012/7/21 create by Karl Chen<br>
 */
@Repository("paymentCanceledReasonDao")
public class PaymentCanceledReasonHibernateDao extends BaseHibernateDao<PaymentCanceledReason, Long> implements PaymentCanceledReasonDao {
    
    @SuppressWarnings("unchecked")
    public PaginatedList<PaymentCanceledReason> queryPaginationByPaymentCanceledReason(PaymentCanceledReason paymentCanceledReason, PaginatedList<PaymentCanceledReason> paginationConfig)
    {
        PaginatedList<PaymentCanceledReason> result = new PaginatedList<PaymentCanceledReason>();
        result.setPageSize(paginationConfig.getPageSize());
        result.setPageNumber(paginationConfig.getPageNumber());
        result.setSortCriterion(paginationConfig.getSortCriterion());
        result.setSortDirection(paginationConfig.getSortDirection());
        
        result.setFullListSize(super.countByDetachedCriteria(createDetachedCriteriaByRealAccount(paymentCanceledReason)));


        /* 筆數大於0 */
        if (0 < result.getFullListSize())
        {
            DetachedCriteria criteria = createDetachedCriteriaByRealAccount(paymentCanceledReason);

            /* 排序 */
            if (null != paginationConfig.getSortCriterion())
            {
                /* 由小到大 */
                if (PaginatedList.SORT_DIRECTION_ASC == paginationConfig.getSortDirection())
                {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
                /* 由大到小 */
                else
                {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
            }

            /* the maximum number of results */
            int maxResults = result.getPageSize();
            /* the first result to retrieve, numbered from 0 */
            int firstResult = result.getFirstIndexForCurrentPage();
            result.setPaginatedList(this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults));
        }

        return result;
    }
    
    protected DetachedCriteria createDetachedCriteriaByRealAccount(PaymentCanceledReason entity)
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        Example example = Example.create(entity);
        example.enableLike(MatchMode.START);
        example.excludeZeroes();
        criteria.add(example);
        
        if (null != entity.getGroup())
        {
            DetachedCriteria detachedCriteria = criteria.createCriteria("group");

            /* PK 有值，需加入為查詢條件 */
            if (null != entity.getGroup().getGroupPk())
            {
                detachedCriteria.add(Restrictions.idEq(entity.getGroup().getGroupPk()));
            }

            /* Group 其他查詢條件 */
            Example e = Example.create(entity.getGroup());
            e.enableLike(MatchMode.START);
            e.excludeZeroes();
            detachedCriteria.add(e);
        }

        return criteria;
    }
    
    @SuppressWarnings("unchecked")
    public List<PaymentCanceledReason> findAllReasonByGroupPk(Long entity)
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        
        criteria.add(Restrictions.eq("group.groupPk", entity));
        
        DetachedCriteria detachedCriteria = criteria.createCriteria("group");
        detachedCriteria.add(Restrictions.idEq(entity));
      
        List<PaymentCanceledReason> rds = this.getHibernateTemplate().findByCriteria(criteria);
        if (0 != rds.size())
            return rds;
        else
            return null;
    }
  
    @SuppressWarnings("unchecked")
	public PaymentCanceledReason findByGroupPkAndCancelCode(Long groupPk, String cancelCode){
    	DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        
        criteria.add(Restrictions.eq("group.groupPk", groupPk));
        criteria.add(Restrictions.eq("canceledNo", cancelCode));
        
        List<PaymentCanceledReason> rds = this.getHibernateTemplate().findByCriteria(criteria);
        if (0 != rds.size())
            return rds.get(0);
        else
            return null;
    }
	
}
